<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Ad;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Ad(), function (Grid $grid) {
            $grid->model()->with(['user','goods'])->orderByDesc('id');

            $grid->column('id')->sortable();
            $grid->column('user.tiktokno', trans('user.fields.tiktokno'))->display(function ($var){
                return $this->user_id."({$var})";
            });
            $grid->column('goods.name', trans('good.fields.name'))->width(300);
            //$grid->column('ads_config_id');
            $grid->column('amount')->display(function ($var) {
                return $var / 100;
            });
            /*
            $grid->column('status')->display(function ($var){
                return \App\Models\Ad::STATUS_MAP[$var];
            });
            */
            $grid->column('click');
            $grid->column('finish_click');
            $grid->column('created_at');

            $grid->quickSearch(['user_id','user.tiktokno']);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('user.id', trans('user.fields.id'));
                $filter->like('user.tiktokno', trans('user.fields.tiktokno'));

            });


            $grid->disableRowSelector();
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Ad(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('goods_id');
            $show->field('ads_config_id');
            $show->field('amount');
            $show->field('status');
            $show->field('click');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Ad(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('goods_id');
            $form->text('ads_config_id');
            $form->text('amount');
            $form->text('status');
            $form->text('click');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
